package com.toggery.POS.controller;

import com.toggery.POS.entity.Category;
import com.toggery.POS.service.CategoryService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by LJH on 2018/3/18
 */
@RestController
//默认返回json格式
@RequestMapping("/category")
@Api(tags = "服装种类数据")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @GetMapping("/list")
    public Map findAllCategory(){
        Map map = new HashMap() ;
        map.put("allCategory",categoryService.findAll());
        return map;
    }

    @GetMapping("/findbyname/{name}")
    public Map findByName(@PathVariable String name){
        Map map = new HashMap() ;
        map.put("allCategory",categoryService.findByName(name));
        return map;
    }

    @GetMapping("/findlikename/{name}")
    public Map findLikeName(@PathVariable String name){
        Map map = new HashMap() ;
        map.put("allCategory",categoryService.findLikeName(name));
        return map;
    }

    @GetMapping("/deleteById/{id}")
    public Map deleteById(int id){
        Map map = new HashMap();
        if(categoryService.deleteById(id)){
            map.put("result",true);
            map.put("msg","删除成功");
        }else {
            map.put("result",false);
            map.put("msg","删除失败");
        }
        return map;
    }

    @PostMapping("/update")
    public Map updateCategory(Category category){
        HashMap map = new HashMap();
        if(categoryService.updateCategory(category)){
            map.put("result",true);
            map.put("result","更新成功");
        }else {
            map.put("result",false);
            map.put("result","更新失败");
        }
        return map;
    }

    @PostMapping("/save")
    public Map saveCategory(Category category){
        HashMap map = new HashMap();
        if(categoryService.save(category)){
            map.put("result",true);
            map.put("result","保存成功");
        }else {
            map.put("result",false);
            map.put("result","保存失败");
        }
        return map;
    }

}
